At some point while creating your game it will be necessary to store information externally and be able to retrieve this information. For example, you may wish to store login details, or hi-scores, or maybe the progression of the player through your levels. Whatever the reason, you will need to know how to use the different file functions that are available to you when working with GameMaker Studio 2.
It is very important to realise that GameMaker Studio 2 is Sandboxedby default, meaning that there are a number of limits and minor complications when using the file system functions that are available, and it is worth noting too that the file system will work slightly differently depending on the platform you are targeting. For more information this, please see the following section:
You can turn off sandboxing on the desktop targets (Windows, macOS and Ubuntu (Linux)) from the Game Options for the chosen platform, meaning that you can then save and load from anywhere, within the limits of the OS permissions.
If you have been granted permission by Steam to use their dedicated API with your game, you will find that there is also a new set of file functions available to you. These are explained in the Steam API section of this manual.
Finally, you should also note that there are some distinct differences between certain target platforms which whould be taken into account when using the file functions. These are:
- iOS: On iOS, if you are loading an included file from a folder, ie: with a path something like "Backgrounds/background1.png", you do NOT need to include the folder as part of the path and simply supplying the file name is sufficient. On all other platforms you must supply the folder(s) as part of the path to the file.
- macOS and Ubuntu (Linux): On the macOS and Ubunutu, filenames and paths are case sensitive on these platforms, and so to prevent any potential issues, GameMaker Studio 2 will automatically make all names/paths use lower case by default.
- HTML5: On HTML5, saving and loading to and from local storage will work as explained within the different function pages, however if you are loading a file from a server, you must only use the function buffer_load_async() as loading synchronously has been deprecated on most browsers and will eventually be obsoleted.
The following sections all deal with different types of file
handling:
Information on the different file functions can be found in the following pages:
Obviously, there will be moments when you need to reference the directory in which files are store and GameMaker Studio 2 also has some special functions relating to the different possible directories that you may need:
When dealing with external files, there is always the possibility that the end user could open and change the information that they contain, and so change your game. This can result in broken game-play elements or fraudulent scores on-line (for example), and so GameMaker Studio 2 provides you with some basic encoding functions as well as functions to perform md5 checks on strings and files to make sure that they have maintained their integrity before being used. There are also two functions supplied for encoding and decoding JSON (JavaScript Object Notation) format strings.
- base64_encode
- base64_decode
- json_encode
- json_decode
- md5_string_utf8
- md5_string_unicode
- md5_file
- sha1_string_utf8
- sha1_string_unicode
- sha1_file
- zip_unzip
- load_csv
NOTE: Encoding is NOT encryption! A base64 encoding renders the file unreadable to the naked eye and will require an effort on behalf of the user to decode, but it is not secure from hacking. It is recommended that you mix those functions with your own encryption (there are many forms of encryption and scripts are available on the internet).